import {message} from 'antd'
import {addCouponGroup, deleteCouponGroup, editCouponGroup, queryCouponGroup} from '../services/api';


export default {
    namespace: 'coupon_group',

    state: {
        data: {list: [], pagination: {size: 10, current: 1, total: 0}},
        item: {},
        couponGroupAddModal: false,
        loading: false,
        confirmLoading: false,
    },

    effects: {
        * fetch({payload}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(queryCouponGroup, payload);
            yield put({
                type: 'save',
                payload: {pagination: response.page, list: response.data},
            });
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },

        * remove({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(deleteCouponGroup, payload);
            if (response.status) {
                message.success('删除成功');
                if (callback) {
                    callback();
                }
            } else {
                message.success(response.message);
            }
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        * add({payload, callback}, {call, put}) {
            yield put({
                type: 'changeConfirmLoading',
                payload: true,
            });
            const response = yield call(addCouponGroup, payload);
            if (response.status) {
                message.success('添加成功');
                if (callback) {
                    callback();
                }
            } else {
                message.success(response.message);
            }
            yield put({
                type: 'changeConfirmLoading',
                payload: false,
            });
        },
        * status({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(editCouponGroup, payload);
            if (response.status) {
                message.success('编辑成功');
                if (callback) {
                    callback();
                }
            } else {
                message.success(response.message);
            }
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        // *fetchSalesData(_, { call, put }) {
        //   const response = yield call(fakeChartData);
        //   yield put({
        //     type: 'save',
        //     payload: {
        //       salesData: response.salesData,
        //     },
        //   });
        // },
    },

    reducers: {
        save(state, {payload}) {
            return {
                ...state,
                data: payload,
            };
        },
        saveItem(state, {payload}) {
            return {
                ...state,
                item: payload,
            };
        },
        changeVisible(state, {payload}) {
            return {
                ...state,
                couponGroupAddModal: payload,
            };
        },
        changeConfirmLoading(state, {payload}) {
            return {
                ...state,
                confirmLoading: payload,
            };
        },
        changeLoading(state, {payload}) {
            return {
                ...state,
                loading: payload,
            };
        }
    },
};
